package com.myd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myd.dao.StandardDataDao;
import com.myd.model.StandardData;
import com.myd.service.StandardDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.*;

@Service
public class StandardDataServiceImpl implements StandardDataService {
    @Autowired
    private StandardDataDao standardDataDao;
    private static final ExecutorService executorSerivce = Executors.newFixedThreadPool(10);
    private static CountDownLatch countDownLatch;

    @Override
    public boolean insertData(StandardData standardData) {

        int insert = standardDataDao.insert(standardData);

        return insert > 0 ? true : false;
    }

    @Override
    public boolean inserExcelData(List<StandardData> list) {
        try {
            long start = System.currentTimeMillis();
            countDownLatch = new CountDownLatch(list.size());
            for (StandardData standardData : list) {
                executorSerivce.submit(new Runnable() {
                    @Override
                    public synchronized void run() {
                        standardDataDao.insert(standardData);
                        countDownLatch.countDown();
                    }
                });
            }
            countDownLatch.await();
            System.out.println(System.currentTimeMillis() - start);
        } catch (Exception e) {
            System.out.println("失败");
            return false;
        }
        return true;
    }

    @Override
    public List<StandardData> selectList(int year) {
        List<StandardData> list = standardDataDao.selectList(new QueryWrapper<StandardData>()
                .eq("year", year)
        );
        return list;
    }

    @Override
    public List<StandardData> searchByStation_no(int station_no) {
        return standardDataDao.selectList(new QueryWrapper<StandardData>()
                .eq("station_no", station_no)
                .orderByAsc("month", "day", "hour"))
                ;
    }

    @Override
    public List<StandardData> searchByTime(String station, String year) {
        List<StandardData> list = standardDataDao.selectList(new QueryWrapper<StandardData>()
                .eq("station_no", station)
                .eq("year", year)
                .orderByAsc("month", "day", "hour")
        );
        return list;
    }
}
